package org.meow.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.meow.constant.JwtConstant;
import org.meow.dto.UserLoginDTO;
import org.meow.entity.User;
import org.meow.properties.JwtProperties;
import org.meow.result.Result;
import org.meow.service.UserService;
import org.meow.utils.JwtUtil;
import org.meow.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * &#064;Date: 24/05/14-13:33
 * &#064;Description:
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户模块", description = "用户模块相关接口")
public class UserController {

  @Autowired
  private UserService   userService;
  @Autowired
  private JwtProperties jwtProp;

  @PostMapping("/login")
  @Operation(summary = "用户登录")
  public Result login(@RequestBody UserLoginDTO userLoginDTO) {
    User                user   = userService.login(userLoginDTO);
    Map<String, Object> claims = new HashMap<>();
    claims.put(JwtConstant.USER_ID, user.getId());
    String token = user.getIsSuper()
        ? JwtUtil.createJwt(jwtProp.getAdminSecretKey(), jwtProp.getAdminTtl(), claims)
        : JwtUtil.createJwt(jwtProp.getUserSecretKey(), jwtProp.getUserTtl(), claims);
    UserLoginVO userLoginVO = UserLoginVO.builder()
                                         .id(user.getId())
                                         .username(user.getUsername())
                                         .token(token)
                                         .build();
    return Result.success(userLoginVO);
  }


}
